package com.example.mycityapp;

import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.mycityapp.util.ImageDownloader;
import com.example.mycityapp.util.StringReplacer;

import android.R.string;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public class PostViewPage extends Activity {
	public static PostViewPage instance;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.postviewpage);
		instance = PostViewPage.this;
		String url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/getPostByPostId?postId=" + ProfilePage.postViewPostId;
		JSONParser jParser = new JSONParser();
		String postUserId = "";
		String json = jParser.doInBackground(url);
		try {
			JSONObject object = new JSONObject(json);
			String postContent = object.getString("postContent"); 
			String postDate = object.getString("postDate"); 
			String postType = object.getString("postType"); 
			String tags = object.getString("tags");
			String userId = object.getString("userId");
			postUserId = userId;
			String postLocation = object.getString("locationName");
			url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/getUserById?userId=" + userId;
			json = jParser.doInBackground(url);
			JSONObject userObject = new JSONObject(json);
			String postUserFirstName = userObject.getString("firstName");
			String postUserLastName = userObject.getString("lastName");
			
			if (postType.contains("2"))
			{
				ImageView imageView = (ImageView) findViewById(R.id.postviewimageView);
				int specialCharIndex = postContent.indexOf("|");
				
				String imgurId = postContent.substring(specialCharIndex + 1);
				postContent = postContent.substring(0, specialCharIndex);
				ImageDownloader.download("http://i.imgur.com/"+imgurId+".jpg", imageView, false);
			}
			TextView textView = (TextView) findViewById(R.id.postviewpostcontenttext);
			textView.setText(postUserFirstName + " " + postUserLastName + ":\n " + postContent + "\n " + "(" + postDate + ")\n" + tags + "\nLocation: " + postLocation);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		ImageButton diamondButton = (ImageButton) findViewById(R.id.postviewlikebutton);
		diamondButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/giveDiamond?postId=" + ProfilePage.postViewPostId + "&userId=" + MainActivity.userId;
				JSONParser jParser = new JSONParser();
				jParser.doInBackground(url);
				ImageButton diamondButton = (ImageButton) findViewById(R.id.postviewlikebutton);
				diamondButton.setImageResource(R.drawable.heart_full);
				diamondButton.setEnabled(false);
			}
		});
		
		url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/isPostDiamonded?postId=" + ProfilePage.postViewPostId + "&userId=" + MainActivity.userId;
		json = jParser.doInBackground(url);
		
		if (json.contains("false"))
		{
			diamondButton.setImageResource(R.drawable.heart_empty);
		}
		else
		{
			diamondButton.setImageResource(R.drawable.heart_full);
			diamondButton.setEnabled(false);
		}
		
		
		LinearLayout postviewcommentlayout = (LinearLayout) findViewById(R.id.postviewcommentlayout);
		
		url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/getCommentsOfPost?postId=" + ProfilePage.postViewPostId;
		json = jParser.doInBackground(url);
		try {
			JSONArray comments = new JSONArray(json);

			for (int i = 0; i < comments.length(); i++)
			{
				LinearLayout postViewSingleCommentLayout = new LinearLayout(this);
				postviewcommentlayout.addView(postViewSingleCommentLayout);
				postViewSingleCommentLayout.setOrientation(LinearLayout.HORIZONTAL);
				postViewSingleCommentLayout.setGravity(Gravity.CENTER);
				postViewSingleCommentLayout.setId(i);
				

				JSONObject object = comments.getJSONObject(i);
				String commentContent = object.getString("commentContent");
				String commentUserId = object.getString("userId");
				int commentHasStar = object.getInt("hasStar");
				final String commentId = object.getString("commentId");
				
				ImageButton starButton = new ImageButton(this);
				
				if (commentHasStar == 0)
				{
					starButton.setImageResource(R.drawable.star_empty);
				}
				else
				{
					starButton.setImageResource(R.drawable.star_full);
				}
				postViewSingleCommentLayout.addView(starButton);
				starButton.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						String urlStar = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/giveStar?commentId="+commentId;
						JSONParser jParserStar = new JSONParser();
						/*String jsonStar = */jParserStar.doInBackground(urlStar);
						Intent intent = getIntent();
					    finish();
					    startActivity(intent);
					}
				});
				if (postUserId.compareTo(MainActivity.userId) != 0)
				{
					starButton.setEnabled(false);
				}
				
				url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/getUserById?userId="+commentUserId;
				json = jParser.doInBackground(url);
				JSONObject userObject = new JSONObject(json);
				String commentUserName = userObject.getString("firstName") + " " + userObject.getString("lastName");
				
				TextView commentContentTextView = new TextView(this);
				commentContentTextView.setText(commentUserName + ": " + commentContent);
				commentContentTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
				commentContentTextView.setGravity(Gravity.CENTER);
				commentContentTextView.setId(i);
				commentContentTextView.setTextColor(Color.WHITE);
				commentContentTextView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
				postViewSingleCommentLayout.addView(commentContentTextView);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Button postviewcommentbutton = (Button) findViewById(R.id.postviewcommentbutton);
		postviewcommentbutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText postviewcommenteditText = (EditText) findViewById(R.id.postviewcommenteditText);
				String newcommentcontent = postviewcommenteditText.getText().toString();
				newcommentcontent = URLEncoder.encode(newcommentcontent);
				String url = "http://titan.cmpe.boun.edu.tr:8087/MyCity/services/user/commentOnPost?commentContent="
						+ newcommentcontent + "&userId=" + MainActivity.userId 
						+ "&postId=" + ProfilePage.postViewPostId;
				JSONParser jParser = new JSONParser();
				String json = jParser.doInBackground(url);
				if (!json.contains("-1"))
				{
					postviewcommenteditText.setText("");
				}
			}
		});
		//
	}
}
